package com.tangtang.core.validator;

import com.tangtang.core.CheckUtil;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;


/**
 *
 */
public class MustMatchValidator implements ConstraintValidator<MustMatch, String> {
    MustMatch must;

    @Override
    public void initialize(MustMatch constraintAnnotation) {
        this.must = constraintAnnotation;
    }


    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if (s == null) {
            return true;
        }
        switch (must.pattern()) {
            case EMAIL:
                return CheckUtil.isEmail(s);
            case IP:
                return CheckUtil.isIP(s);
            case URL:
                return CheckUtil.isUrl(s);
            case TELEPHONE:
                return CheckUtil.isTelephone(s);
            case POSTCODE:
                return CheckUtil.isPostCode(s);
            case MOBILE:
                return CheckUtil.isMobile(s);
            case IDCARD:
                return CheckUtil.isIDcard(s);
            case DECIMAL:
                return CheckUtil.isDecimal(s);
            case MONTH:
                return CheckUtil.isMonth(s);
            case DAY:
                return CheckUtil.isDay(s);
            case DATE:
                return CheckUtil.isDate(s);
            case NUMBER:
                return CheckUtil.isNumber(s);
            case UPCHAR:
                return CheckUtil.isUpChar(s);
            case LOWCHAR:
                return CheckUtil.isLowChar(s);
            case LETTER:
                return CheckUtil.isLetter(s);

        }
        return true;
    }
}
